package weaver.interfaces.ybcn.duss.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.ybcn.duss.utils.T100Util;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2024-04-07
 * @Description: 客户主数据新增申请流程调用cwssp042新增修改客户资料接口
 * @ModifyLog: OA-T100，OA将客户主数据信息传给T100；
 **/
public class WeaKhzsjxzsqlcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaKhzsjxzsqlcAction>>>>>requestid=" + requestid);
        try {
            RecordSet rs = new RecordSet();
            T100Util t100Util = new T100Util();
            rs.execute("select * from " + tablename + " where requestid=?", requestid);
            if (rs.next()) {
                JSONObject payloadJson = new JSONObject();
                JSONObject stddataJson = new JSONObject();
                JSONObject parameterJson = new JSONObject();
                JSONArray masterList = new JSONArray();
                JSONObject masterJson = new JSONObject();
                //账户组
                masterJson.put("account_no", Util.null2String(rs.getString("KTOKD")));
                //客户
                masterJson.put("pmaa002", "2");
                //税号	税号
                masterJson.put("pmaa003", Util.null2String(rs.getString("sh")));
                //法人类型
                masterJson.put("pmaa004", "1");
                //全称	客户名称
                masterJson.put("pmaal003", Util.null2String(rs.getString("khmc")));
                //简称	客户简称
                masterJson.put("pmaal004", Util.null2String(rs.getString("khjc")));
                String VKGRP = Util.null2String(rs.getString("VKGRP"));
                //负责业务人员  销售组
                masterJson.put("pmaa096",VKGRP );
                //负责业务部门  传销售组对应人员的部门编码
                masterJson.put("pmaa097", Util.null2String(t100Util.getUserInfoByid(VKGRP).get("departmentcode")));
                //客户分类		客户分类
                masterJson.put("pmaa090", Util.null2String(rs.getString("khdj")));
                //客户生命周期	为客户或两者皆是时不可为空；默认传是
                masterJson.put("pmaa093", "是");
                String sqr = Util.null2String(rs.getString("sqr"));
                //资料所有者	申请人
                masterJson.put("pmaaownid", sqr);
                //资料录入者  申请人
                masterJson.put("pmaacrtid", sqr);
                //资料创建日		申请日期
                masterJson.put("pmaacrtdt", Util.null2String(rs.getString("sqrq")));
                //交易币别	为客户或两者皆是时不可为空	币种
                masterJson.put("pmab083", Util.null2String(rs.getString("bz")));
                //交易税别	为客户或两者皆是时不可为空	税分类
                masterJson.put("pmab084", Util.null2String(rs.getString("sfl")));
                //收款条件	付款条件
                masterJson.put("pmab087", Util.null2String(rs.getString("fktj")));
                //销售分类	分销渠道
                masterJson.put("pmab089", Util.null2String(rs.getString("VTWEG")));
                //行业别		行业
                masterJson.put("pmaa023", Util.null2String(rs.getString("KVGR1")));
                //所属集团	集团
                masterJson.put("pmaa006", Util.null2String(rs.getString("KVGR2")));
                //企业额度	信贷限额
                masterJson.put("pmaa055", Util.null2String(rs.getString("xded")));
                //省份		省份
                masterJson.put("pmaaud001", Util.null2String(rs.getString("sf")));
                //销售地区	销售地区
                masterJson.put("pmaaud002", Util.null2String(rs.getString("BZIRK")));
                //客服人员	客服
                masterJson.put("pmaaud003", Util.null2String(rs.getString("KVGR3")));
                //铝价取值类型		铝价取值类型
                masterJson.put("pmabud001", Util.null2String(rs.getString("ljqzlx")));
                //计算逻辑		计算逻辑
                masterJson.put("pmabud002", Util.null2String(rs.getString("jslj")));
                //基准日		基准日
                masterJson.put("pmabud003", Util.null2String(rs.getString("jzr")));
                //过量交货限度		过量交货限度
                masterJson.put("pmaaua001", Util.null2String(rs.getString("UEBTO")));
                //交货不足限度		交货不足限度
                masterJson.put("pmaaua002", Util.null2String(rs.getString("UNTTO")));
                //交货期限		交货期限
                masterJson.put("pmaaud011", Util.null2String(rs.getString("KVGR4")));
                //备注		备注说明
                masterJson.put("ooff013", Util.null2String(rs.getString("bzsm")));

                JSONArray addressDetailList = new JSONArray();
                JSONObject addressDetailJson1 = new JSONObject();
                //简要编号
                addressDetailJson1.put("oofb019","1");
                //地址类型
                addressDetailJson1.put("oofb008","5");
                //国家/地区别		国家
                addressDetailJson1.put("oofb012",Util.null2String(rs.getString("gj")));
                //地址		地址1
                addressDetailJson1.put("oofb017",Util.null2String(rs.getString("dz")));
                addressDetailList.add(addressDetailJson1);
                JSONObject addressDetailJson2 = new JSONObject();
                //简要编号
                addressDetailJson2.put("oofb019","2");
                //地址类型
                addressDetailJson2.put("oofb008","1");
                //国家/地区别		国家
                addressDetailJson2.put("oofb012",Util.null2String(rs.getString("gj")));
                //地址		地址2
                addressDetailJson2.put("oofb017",Util.null2String(rs.getString("STR_SUPPL1")));
                addressDetailList.add(addressDetailJson2);
                JSONObject addressDetailJson3 = new JSONObject();
                //简要编号
                addressDetailJson3.put("oofb019","3");
                //地址类型
                addressDetailJson3.put("oofb008","3");
                //国家/地区别		国家
                addressDetailJson3.put("oofb012",Util.null2String(rs.getString("gj")));
                //地址		地址3
                addressDetailJson3.put("oofb017",Util.null2String(rs.getString("STR_SUPPL2")));
                addressDetailList.add(addressDetailJson3);
                masterJson.put("address_detail", addressDetailList);

                JSONArray communicationDetailList = new JSONArray();
                JSONObject communicationDetailJson1 = new JSONObject();
                //通信类型
                communicationDetailJson1.put("oofc008","2");
                //通信内容		电话
                communicationDetailJson1.put("oofc012",Util.null2String(rs.getString("TELF1")));
                communicationDetailList.add(communicationDetailJson1);
                JSONObject communicationDetailJson2 = new JSONObject();
                //通信类型
                communicationDetailJson2.put("oofc008","2");
                //通信内容		电话2
                communicationDetailJson2.put("oofc012",Util.null2String(rs.getString("lxrdh")));
                communicationDetailList.add(communicationDetailJson2);
                JSONObject communicationDetailJson3 = new JSONObject();
                //通信类型
                communicationDetailJson3.put("oofc008","4");
                //通信内容		邮箱
                communicationDetailJson3.put("oofc012",Util.null2String(rs.getString("yx")));
                communicationDetailList.add(communicationDetailJson3);
                masterJson.put("communication_detail", communicationDetailList);

                JSONArray pmadDetailList = new JSONArray();
                JSONObject pmadDetailJson = new JSONObject();
                //收款条件		国际贸易条款
                pmadDetailJson.put("pmad002",Util.null2String(rs.getString("INCO1")));
                //交易类型	1:付款 2:收款
                pmadDetailJson.put("pmad003","1");
                pmadDetailList.add(pmadDetailJson);
                masterJson.put("pmad_detail", pmadDetailList);

                JSONArray bankDetailList = new JSONArray();
                JSONObject bankDetailJson = new JSONObject();
                //银行编号	开票银行名称
                bankDetailJson.put("pmaf002",Util.null2String(rs.getString("NAME_CO")));
                //银行账户	开票银行账号
                bankDetailJson.put("pmaf003",Util.null2String(rs.getString("CITY2")));
                bankDetailList.add(bankDetailJson);
                masterJson.put("bank_detail", bankDetailList);

                masterList.add(masterJson);
                parameterJson.put("master", masterList);
                stddataJson.put("parameter", parameterJson);
                payloadJson.put("std_data", stddataJson);
                writeLog(">>>>>WeaKhzsjxzsqlcAction>>>>>payloadJson=" + payloadJson);
                JSONObject resultJson = t100Util.ClentCreat(payloadJson, "WeaKhzsjxzsqlcAction", requestid, src);
                writeLog(">>>>>WeaKhzsjxzsqlcAction>>>>>resultJson=" + resultJson);
                String message = Util.null2String(resultJson.getString("message"));
                if ("success".equals(message)) {
                    JSONObject dataJson = JSONObject.parseObject(resultJson.getString("data"));
                    JSONObject retPayloadJson = JSONObject.parseObject(dataJson.getString("payload"));
                    JSONObject retStddataJson = JSONObject.parseObject(retPayloadJson.getString("std_data"));
                    JSONObject retParameterJson = JSONObject.parseObject(retStddataJson.getString("parameter"));
                    int failnum = Util.getIntValue(retParameterJson.getString("failnum"), 0);
                    if (failnum > 0) {
                        JSONArray failreturnList = JSONArray.parseArray(retParameterJson.getString("fail_return"));
                        String errorMsg = failreturnList.getJSONObject(0).getString("error_msg");
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent("调用T100系统接口返回:" + errorMsg);
                        return Action.FAILURE_AND_CONTINUE;
                    } else {
                        JSONArray successreturnList = JSONArray.parseArray(retParameterJson.getString("success_return"));
                        String successMsg = successreturnList.getJSONObject(0).getString("success_msg");
                        rs.execute("update "+tablename+" set PARTNER='"+successMsg+"' where requestid=?",requestid);
                    }
                } else {
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent("调用T100系统接口返回:" + message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaKhzsjxzsqlcAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}